/// 
/// Directives
/// -------------------------------------------------------------------------------------------------------------------
/// <reference path="_references.ts" />
'use strict';
angular.module('app.directives', [])
    //.directive('appVersion', [<any> 'version', function (version) {
    //    return function (scope, elm, attrs) {
    //        elm.text(version);
    //    };
    //}])
 

.directive("myCheckboxGroup", function ($compile, _) {
    return {
       // require: 'ngModel',
        //  restrict: "AEC",
        transclude: true,
        scope: {
            destarray: "=",  // the source of all the checkbox values
            fieldobj: "=",  // the array the values came from
           // ngModel: "=",
            validationfunc: "&"   // the function to be called for validation (optional)
        },
        link: function ($scope, elem, attrs, ngModelController) {
            if ($scope.destarray !== undefined && $scope.fieldobj !== undefined) {
                var rec3 = _.findWhere($scope.destarray, {
                    hazardTypeID: $scope.fieldobj 
                });
                if (rec3 !== undefined) {
                    elem[0].checked = true;
                    $scope.$apply();
                }
            }
            elem.bind('change', function () {
                var rec = _.findWhere($scope.destarray, {
                    hazardTypeID: $scope.fieldobj
                });
                if (elem[0].checked) {
                    if (rec == undefined) {
                        //if there are no destination items
                        //new one up and add locationid and fieldobj
                        if ($scope.destarray.length > 0) {
                            var rec = $scope.destarray[0];

                            $scope.destarray.push({
                                id: 0,
                                locationID: rec.locationID,
                                location: rec.location,
hazardTypeID: $scope.fieldobj});
                            $scope.$parent.$parent.location.locationHazards.push({
                                id: 0,
                                locationID: rec.locationID,
                                location: rec.location,
                                hazardTypeID: $scope.fieldobj,
                                lookup: null,
                            });
                            $scope.$apply();
                            $scope.$parent.$parent.form.$setDirty();
                        }
                        else {
                            $scope.destarray.push({});
                            $scope.destarray[0].hazardTypeID = $scope.fieldobj;
                            $scope.destarray[0].location = $scope.$parent.$parent.location;
                            $scope.destarray[0].locationID = $scope.$parent.$parent.location.id;
                            var rec = $scope.destarray[0];
                            $scope.$parent.$parent.location.locationHazards.push(rec);
                            $scope.$apply();
                            $scope.$parent.$parent.form.$setDirty();
                        }
                    }
                }
                else {
                    if (rec !== undefined) {
                     
                        var log;
                        $scope.destarray.splice(rec.id, 1);
                        $scope.$parent.$parent.location.locationHazards.forEach(function (element, index, array)
                        {
                            if (element.id == rec.id) {

                                $scope.$parent.$parent.location.locationHazards.splice(index, 1);
                                
                            }
                        })
                        $scope.$apply();

                    }
                }
            });
        }
    };
})

.directive("loadingWidget", [<any> '_START_REQUEST_', <any> '_END_REQUEST_', function (_START_REQUEST_, _END_REQUEST_) {
    return {
        restrict: "A",
        link: function (scope, element) {
            // hide the element initially
            element.hide();

            scope.$on(_START_REQUEST_, function () {
                // got the request start notification, show the element
                element.show();
            });

            scope.$on(_END_REQUEST_, function () {
                // got the request end notification, hide the element
                element.hide();
            });
        }
    };
}])
    ;